﻿using Cinch;
using FR.SAFTAnalyserLite.SAFT;
using FR.SAFTAnalyserLite.Services;
using FR.SAFTAnalyserLite.ViewModels.Pane;
using MEFedMVVM.Common;
using MEFedMVVM.ViewModelLocator;
using System.ComponentModel.Composition;
using System.IO;
using System.Xml.Linq;

namespace FR.SAFTAnalyserLite.ViewModels.Tools
{
    public class ConvertPemKeyViewModel : ToolViewModel, ITool
    {
        IOpenFileService openFileService;

        public ConvertPemKeyViewModel()
            : base("Ferramentas")
        {
            this.openFileService = ServiceResolver.Instance.OpenFileService;
            if (!Designer.IsInDesignMode)
            {
                //Commands
                DoOpenPemFileCommand = new SimpleCommand<object, object>(CanOpenPemFile, OpenPemFile);
            }
        }

        string pemFileName;
        public string PemFileName
        {
            get { return pemFileName; }
            set
            {
                if (File.Exists(value))
                {
                    pemFileName = value;
                    NotifyPropertyChanged(ObservableHelper.CreateArgs<ConvertPemKeyViewModel>(x => x.PemFileName));
                }
            }
        }

        string pemText;
        public string PemText
        {
            get { return pemText; }
            set
            {
                pemText = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<ConvertPemKeyViewModel>(x => x.PemText));
            }
        }

        string rsaSetings;
        public string RsaSetings
        {
            get { return rsaSetings; }
            set
            {
                rsaSetings = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<ConvertPemKeyViewModel>(x => x.RsaSetings));
            }
        }

        #region Commands
        public SimpleCommand<object, object> DoOpenPemFileCommand { get; private set; }
        private void OpenPemFile(object args)
        {
            if (openFileService != null)
                this.PemFileName = openFileService.OpenFile("Pem Files (*.pem *.txt)|*.pem;*.txt");

            ConvertPemFile();
        }

        private bool CanOpenPemFile(object args)
        {
            return true;
        }
        #endregion

        private void ConvertPemFile()
        {
            if (File.Exists(this.PemFileName))
            {
                this.PemText = File.ReadAllText(this.PemFileName);

                RSAKeys rsa = new RSAKeys();
                rsa.DecodePEMKey(this.PemText);

                string key = string.IsNullOrEmpty(rsa.PublicKey) ? rsa.PrivateKey : rsa.PublicKey;

                //Usar XDocument porque o ToString formata o xml
                XDocument xmlDoc = XDocument.Parse(key);

                this.RsaSetings = xmlDoc.ToString();
            }
        }

        PaneLocation preferredLocation = new PaneLocation { Location = PaneLocation.LEFT };
        public PaneLocation PreferredLocation
        {
            get { return preferredLocation; }
            set { preferredLocation = value; }
        }

        bool isAutoHidden = true;
        public bool IsAutoHidden
        {
            get { return isAutoHidden; }
            set { isAutoHidden = value; }
        }
    }
}
